
#include "init.h"
#include "../teximage/teximage.h"

#include "m_menu.h"

////////////////////////////////////////////////////////////////////////////////
struct cfgcolor_menu : Menu
{
    cfgcolor_menu()
    {
        *(uint32*)prgb = 0;
        *(uint32*)srgb = 0;

        add_bar( 0, "major red", &prgb[2] );
        add_bar( 1, "major green", &prgb[1] );
        add_bar( 2, "major blue", &prgb[0] );

        add_space(16);

        add_bar( 0, "minor red", &srgb[2] );
        add_bar( 1, "minor green", &srgb[1] );
        add_bar( 2, "minor blue", &srgb[0] );

        add_space(64);

        add_entry( "ok" );
        add_entry( "cancel" );
        set_escape_code( "cancel" );
    }

    void set_colors( uint32 pri, uint32 sec )
    {
        *(uint32*)prgb = pri;
        *(uint32*)srgb = sec;
    }

    uint32 get_colpri() const               { return *(const uint32*)prgb; }
    uint32 get_colsec() const               { return *(const uint32*)srgb; }

protected:
    uchar prgb[4];
    uchar srgb[4];
};

////////////////////////////////////////////////////////////////////////////////
void bman_net::draw_cfgcolor( int key )
{
    static int binit=0;
    static cfgcolor_menu menu;

    if(key) {
        token k = menu.onkeypress(key);
        if( k == "cancel" )
        {
            menu.set_colors( CW._profile._colpri, CW._profile._colsec );
            CW.gs.state = C_World::GSC_OPTIONS;
            binit = false;
        }
        else if( k == "ok" )
        {
            CW._profile._colpri = menu.get_colpri();
            CW._profile._colsec = menu.get_colsec();
            CW.save_profile();
            CW.gs.state = C_World::GSC_OPTIONS;
            binit = false;

            CW.save_sample();
        }
        return;
    }

    if(!binit)
    {
        menu.set_colors( CW._profile._colpri, CW._profile._colsec );

        binit = true;
    }

    SCREEN.clear(0x3f3f3f);

    menu.set_sel_colors( menu.get_colpri(), menu.get_colsec() );
    menu.draw();

    uint x = SCREEN.width() - CW._spr_manq._array[0].get_width() - 64;
    uint y = 64+16;

    uint fr = (_Timer.time() / 128) % CW._spr_manq._array.size();

    //SCREEN.draw( back, x, y );
    SCREEN.draw_colorize( CW._spr_manq._array[fr], 0, menu.get_colpri(), menu.get_colsec(), x, y );
}

